﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Application.AppUser.Dto;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
    public class ProductTypeDomainService: IProductTypeDomainService
    {
        private readonly IBaseRepository<ProductType> baseRepository;
        private readonly MyContext myContext;
        public ProductTypeDomainService(IBaseRepository<ProductType> _baseRepository, MyContext _myContext)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ProductType> GetProductType(int Id)
        {
            return await myContext.ProductTypes.FindAsync(Id);
        }


        /// <summary>
        /// 获取商品分类集合
        /// </summary>
        /// <returns></returns>
        //public async Task<HeaderResult<List<PproductType>>> GetProductTypes(int Pid,int State,string Name,int pageIndex,int pageSize)
        //{
        //    try
        //    {
        //        var fen = from a in myContext.Products
        //                  join b in myContext.ProductTypes
        //                  on a.TypeID equals b.ParentID into tp
        //                  from tt in tp.DefaultIfEmpty()
        //                  join c in myContext.PicUrls
        //                  on a.Photo equals c.ID into pi
        //                  from pp in pi.DefaultIfEmpty()
        //                  where (Pid == 0 || a.TypeID.Equals(Pid)) && (State == 0 || a.State.Equals(State)) && (Name == "" || a.ProName.Equals(Name))
        //                  select new PproductType
        //                  {
        //                      ProID=a.ProID,
        //                      Photo=pp.ID==0?"":pp.Photo,
        //                      ProName=tt.ProTypeName,
        //                      Sales=a.Sales,
        //                      Sort=a.Sort,
        //                      State=a.State,
        //                      CreateTime=a.CreateTime
        //                  };
        //        int d = fen.Count();
        //        fen = fen.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        //        HeaderResult<List<PproductType>> result = new HeaderResult<List<PproductType>>
        //        {
        //            IsSucceed = true,
        //            Result = await fen.ToListAsync(),
        //            Total = d,
        //        };
        //        return result;
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}


        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> IsDel(int Id)
        {
            Eval eval = myContext.Evals.Where(x => x.EvalID.Equals(Id)).FirstOrDefault();
            eval.IsDeleted = true;
            return await myContext.SaveChangesAsync();
            //string sql = $"update ProductTypes set IsDeleted = 1 where ProTypeID = {Id}";
            //return myContext.Database.ExecuteSqlCommand(sql);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="productType"></param>
        /// <returns></returns>
        public async Task<int> Upt(ProductType productType)
        {
            return await baseRepository.UpdateEntityAsync(productType);
        }
    }
}
